/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchtongiaoException;
import doan.quanlycanbo.portlet.model.tongiao;
import doan.quanlycanbo.portlet.service.base.tongiaoLocalServiceBaseImpl;

/**
 * 
 * @author Nguyen Tuan Vinh
 */
public class tongiaoLocalServiceImpl extends tongiaoLocalServiceBaseImpl {
	
	public List<tongiao> getAll() throws SystemException{
		return tongiaoPersistence.findAll();
	}
	
	public tongiao addTonGiao(tongiao tongiao1, long userId) throws SystemException{
		long matongiao = CounterLocalServiceUtil.increment(tongiao.class.getName());
		tongiao tongiao2 = tongiaoPersistence.create(matongiao);
		tongiao2.setGroupId(tongiao1.getGroupId());
		tongiao2.setCompanyId(tongiao1.getCompanyId());
		tongiao2.setLanguageId(tongiao1.getLanguageId());
		tongiao2.setTentongiao(tongiao1.getTentongiao());

		try {
			resourceLocalService.addResources(tongiao1.getCompanyId(), tongiao1.getGroupId(), userId, 
					tongiao.class.getName(), tongiao2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return tongiaoPersistence.update(tongiao2, false);
	}
	
	public tongiao updateTonGiao(long matongiao, String tentongiao) throws SystemException, NoSuchtongiaoException{
		tongiao tonGiao = tongiaoPersistence.findByPrimaryKey(matongiao);
		tonGiao.setTentongiao(tentongiao);
		return tongiaoPersistence.update(tonGiao, false);
	}
	
	public tongiao deleteTonGiao(long matongiao) throws SystemException, NoSuchtongiaoException,PortalException{
		tongiao tonGiao = tongiaoPersistence.findByPrimaryKey(matongiao);
		return deleteTonGiao(tonGiao);
	}
	
	public tongiao deleteTonGiao(tongiao tonGiao) throws SystemException,PortalException{
		resourceLocalService.deleteResource(tonGiao.getCompanyId(), tongiao.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, tonGiao.getPrimaryKey());
		return tongiaoPersistence.remove(tonGiao);
	}
}